function bounds = maxsubarray(X)

% O(n) algorithm for solving 1D maximum sub-array problem

X = X(:);

bounds = [1 1];
bestScore = -realmax;
currScore = 0;
begInd = 1;
for curInd = 1:numel(X)
    currScore = currScore + X(curInd);
    
    if currScore < 0
        currScore = 0;
        begInd = curInd+1;
    elseif currScore > bestScore
        bestScore = currScore;
        bounds = [begInd curInd];
    end
end
